package com.handle.gateway;

import com.handle.common.core.enums.CommonEnum;
import com.handle.common.core.utils.DefaultProfileUtil;
import com.handle.gateway.config.ApplicationProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.core.env.Environment;

import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.Collection;

/**
 * 网关启动类
 *
 * @author zq
 */
@EnableDiscoveryClient
@SpringBootApplication
@EnableConfigurationProperties({ApplicationProperties.class})
public class HandleGatewayApplication {

    private static final Logger log = LoggerFactory.getLogger(HandleGatewayApplication.class);

    private final Environment env;

    public HandleGatewayApplication(Environment env) {
        this.env = env;
    }

    @PostConstruct
    public void initApplication() {
        Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
        if (activeProfiles.size() > 1) {
            log.error("您的应用程序配置错误!它不应该同时运行{}配置文件。", activeProfiles);
            CommonEnum.ACTIVE_PROFILE_TO_MANY.assertFail("您的应用程序配置错误!它不应该同时运行{0}配置文件。", activeProfiles.toString());
        }
    }

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(HandleGatewayApplication.class);
        DefaultProfileUtil.addDefaultProfile(app);
        Environment env = app.run(args).getEnvironment();
        DefaultProfileUtil.logApplicationStartup(env);
    }

}
